/**
 * Created by uf040339 on 2016/9/7.
 */

(function ($) {
    $(function () {
        getSystemClock();
    });

    var fillLineInfo = function (resp, codeVal) {
        if (resp['S' + codeVal]) $('#iptS' + codeVal).html(resp['S' + codeVal]);
        if (resp['E' + codeVal]) $('#iptE' + codeVal).html(resp['E' + codeVal]);

        if (resp['TargetOutput' + codeVal]) $('#targetOutput' + codeVal).html(resp['TargetOutput' + codeVal]);
        if (resp['TargetInput' + codeVal]) $('#targetInput' + codeVal).html(resp['TargetInput' + codeVal]);
        if (resp['CurrentOutput' + codeVal]) {
            var $currentOutput = $('#currentOutput' + codeVal);
            $currentOutput.html(resp['CurrentOutput' + codeVal]);
            if (resp['PlannedOutput' + codeVal] > resp['CurrentOutput' + codeVal]) {
                $currentOutput.addClass('text-danger');
            }
            else {
                $currentOutput.removeClass('text-danger');
            }
        }
        if (resp['CurrentInput' + codeVal]) {
            var $currentInput = $('#currentInput' + codeVal);
            $currentInput.html(resp['CurrentInput' + codeVal]);
            if (resp['PlannedInput' + codeVal] > resp['CurrentInput' + codeVal]) {
                $currentInput.addClass('text-danger');
            }
            else {
                $currentInput.removeClass('text-danger');
            }
        }
        if (resp['ForecastOutput' + codeVal]) {
            var $forecastOutput = $('#forecastOutput' + codeVal);
            $forecastOutput.html(resp['ForecastOutput' + codeVal]);
            if (resp['TargetOutput' + codeVal] > resp['ForecastOutput' + codeVal]) {
                $forecastOutput.addClass('text-danger');
            }
            else {
                $forecastOutput.removeClass('text-danger');
            }
        }
        if (resp['ForecastInput' + codeVal]) {
            var $forecastInput = $('#forecastInput' + codeVal);
            $forecastInput.html(resp['ForecastInput' + codeVal]);
            if (resp['TargetInput' + codeVal] > resp['ForecastInput' + codeVal]) {
                $forecastInput.addClass('text-danger');
            }
            else {
                $forecastInput.removeClass('text-danger');
            }
        }
        if (resp['PlannedOutput' + codeVal]) {
            var $plannedOutput = $('#plannedOutput' + codeVal);
            $plannedOutput.html(resp['PlannedOutput' + codeVal]);

            if (resp['CurrentOutput' + codeVal]) {
                var $cpCut = $('#cpCutOutput' + codeVal),
                    cpCutVal = parseInt(resp['CurrentOutput' + codeVal]) - parseInt(resp['PlannedOutput' + codeVal]);
                $cpCut.html(cpCutVal);
                if (cpCutVal < 0) {
                    $cpCut.addClass('text-danger');
                }
                else {
                    $cpCut.removeClass('text-danger');
                }
            }
        }
        if (resp['PlannedInput' + codeVal]) {
            var $plannedInput = $('#plannedInput' + codeVal);
            $plannedInput.html(resp['PlannedInput' + codeVal]);

            if (resp['CurrentInput' + codeVal]) {
                var $cpCut = $('#cpCutInput' + codeVal),
                    cpCutVal = parseInt(resp['CurrentInput' + codeVal]) - parseInt(resp['PlannedInput' + codeVal]);
                $cpCut.html(cpCutVal);
                if (cpCutVal < 0) {
                    $cpCut.addClass('text-danger');
                }
                else {
                    $cpCut.removeClass('text-danger');
                }
            }
        }
    };

    var getSystemClock = function () {
        var url = location.href;
        //var ptn = new RegExp('.*?\\&date=(\\d{8})');
        //var dateNum = url.replace(ptn, '$1');
        var ptn = new RegExp('.*?\\?line_name=([GZ]6)\\&date=(\\d{8})');
        var mtArr = url.match(ptn);
        var data = {
            line_name: mtArr[1],
            date: mtArr[2]
        };
        $.get('/NEP/PSS/api/product_info', data, function (resp) {
            if (resp) {
                var codeArr = [1, 2, 3];
                $.each(codeArr, function(idx, code) {
                    fillLineInfo(resp, code);
                });
            }
        }, 'json');
    };
})(jQuery);
